﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using GalaSoft.MvvmLight.Messaging;
using Miris.HumanMilkAnalyser.WPF.ViewModel;

namespace Miris.HumanMilkAnalyser.WPF.Shared
{
    public class PageBase : Page
    {
        protected Storyboard StoryboardLoadPage;
        protected Storyboard StoryboardUnloadPage;
        protected object NavigationTarget;

        public PageBase()
        {
            Dispatcher.ShutdownStarted += Dispatcher_ShutdownStarted;
            //Loaded += PageBase_Loaded;
            Unloaded += PageBase_Unloaded;

            RegisterMessages();
        }

        /// <summary>
        /// Register all necessary messages.
        /// </summary>
        protected virtual void RegisterMessages()
        {
            RegisterReturnHomeMessage();
            RegisterRequestGotoPageMessage();
            RegisterGoToCreatePatientPageMessage();
            RegisterGoToPatientDataSearchPageMessage();
            RegisterGoToDiagnosticMessageSetupPageMessage();
            RegisterGoToDataManagementPageMessage();
            RegisterGoToFurtherSuggestionPageMessage();
            RegisterGoToBasicConfigurationPageMessage();
            RegisterGoToHelpPageMessage();
            RegisterGoToRegistrationCodePageMessage();
        }

        protected virtual void PageBase_Loaded(object sender, RoutedEventArgs e)
        {
            StoryboardLoadPage = FindResource("StoryboardLoadPage") as Storyboard;
            StoryboardLoadPage.Begin();
        }

        /// <summary>
        /// Initialize the Storyboard for PageUnload.
        /// </summary>
        protected virtual void InitializePageUnloadStoryboard()
        {
            //StoryboardUnloadPage = FindResource("StoryboardUnloadPage") as Storyboard;
            //StoryboardUnloadPage.Completed += StoryboardUnloadPage_Completed;
        }

        protected virtual void StoryboardUnloadPage_Completed(object sender, EventArgs e)
        {
            if (NavigationService != null)
            {
                NavigationService.Navigate(NavigationTarget);
            }
        }

        /// <summary>
        /// Go to the specified page and reset all properties.
        /// </summary>
        /// <param name="navigationTarget">The page will be navigated to.</param>
        protected virtual void GoToPage(PageBase navigationTarget)
        {
            GoToPage(navigationTarget, true);
        }

        /// <summary>
        /// Go to the specified page.
        /// </summary>
        /// <param name="navigationTarget">The page will be navigated to.</param>
        /// <param name="needToResetProperties">If need to reset all properties.</param>
        protected virtual void GoToPage(PageBase navigationTarget, bool needToResetProperties)
        {
            var viewModel = navigationTarget.DataContext as MyViewModelBase;

            if (viewModel != null)
            {
                viewModel.CheckRegisterMessages();

                if (needToResetProperties)
                {
                    viewModel.ResetProperties();
                }
            }

            NavigationTarget = navigationTarget;
            //StoryboardUnloadPage.Begin();

            if (NavigationService != null)
            {
                NavigationService.Navigate(NavigationTarget);
            }
        }

        /// <summary>
        /// Return to the home page and reset all necessary properties.
        /// </summary>
        protected virtual void GoBackToHomePage()
        {
            GoToPage(new MainPage());
        }

        /// <summary>
        /// Clean in View.
        /// </summary>
        protected virtual void Cleanup()
        {
            ViewHelper.Cleanup(this);
        }

        protected virtual void Dispatcher_ShutdownStarted(object sender, EventArgs e)
        {
            Cleanup();
        }

        protected virtual void PageBase_Unloaded(object sender, RoutedEventArgs e)
        {
            Cleanup();
        }

        protected virtual void ButtonCancel_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("确认取消当前操作？", "取消操作", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
            {
                GoBackToHomePage();
            }
        }

        protected virtual void ButtonReturn_Click(object sender, RoutedEventArgs e)
        {
            GoBackToHomePage();
        }

        /// <summary>
        /// Register the ReturnHomeMessage from MainViewModel.
        /// </summary>
        private void RegisterReturnHomeMessage()
        {
            Messenger.Default.Register<int>(this, MainViewModel.ReturnHomeMessageToken, p =>
            {
                GoBackToHomePage();
            });
        }

        /// <summary>
        /// Register RequestGotoPageMessage from MyViewModelBase.
        /// </summary>
        private void RegisterRequestGotoPageMessage()
        {
            Messenger.Default.Register<PageBase>(this, MyViewModelBase.RequestGotoPageMessageToken, p =>
            {
                GoToPage(p);
            });
        }

        /// <summary>
        /// Register GoToCreatePatientPageMessage from MainViewModel
        /// </summary>
        private void RegisterGoToCreatePatientPageMessage()
        {
            Messenger.Default.Register<int>(this, MainViewModel.GoToCreatePatientPageMessageToken, p =>
                {
                    GoToPage(new CreatePatientPage());
                });
        }

        /// <summary>
        /// Register GoToPatientDataSearchPageMessage from MainViewModel
        /// </summary>
        private void RegisterGoToPatientDataSearchPageMessage()
        {
            Messenger.Default.Register<int>(this, MainViewModel.GoToPatientDataSearchPageMessageToken, p =>
            {
                GoToPage(new PatientDataSearchPage());
            });
        }

        /// <summary>
        /// Register the GoToDiagnosticMessageSetupPageMessage from MainViewModel
        /// </summary>
        private void RegisterGoToDiagnosticMessageSetupPageMessage()
        {
            Messenger.Default.Register<int>(this, MainViewModel.GoToDiagnosticMessageReferenceSetupPageMessageToken, p =>
            {
                GoToPage(new DiagnosticMessageReferenceSetupPage());
            });
        }

        /// <summary>
        /// Register the GoToDataManagementPageMessage from MainViewModel
        /// </summary>
        private void RegisterGoToDataManagementPageMessage()
        {
            Messenger.Default.Register<int>(this, MainViewModel.GoToDataManagementPageMessageToken, p =>
            {
                GoToPage(new DataManagementPage());
            });
        }

        /// <summary>
        /// Register the GoToFurtherSuggestionPageMessage from MainViewModel
        /// </summary>
        private void RegisterGoToFurtherSuggestionPageMessage()
        {
            Messenger.Default.Register<int>(this, MainViewModel.GoToFurtherSuggestionPageMessageToken, p =>
            {
                GoToPage(new FurtherSuggestionPage());
            });
        }

        /// <summary>
        /// Register the GoToBasicConfigurationPageMessage from MainViewModel
        /// </summary>
        private void RegisterGoToBasicConfigurationPageMessage()
        {
            Messenger.Default.Register<int>(this, MainViewModel.GoToBasicConfigurationPageMessageToken, p =>
            {
                GoToPage(new BasicConfigurationPage());
            });
        }

        /// <summary>
        /// Register the GoToHelpPageMessage from MainViewModel
        /// </summary>
        private void RegisterGoToHelpPageMessage()
        {
            Messenger.Default.Register<int>(this, MainViewModel.GoToHelpPageMessageToken, p =>
            {
                GoToPage(new HelpPage());
            });
        }

        /// <summary>
        /// Register the GoToRegistrationCodePageMessage from MainViewModel
        /// </summary>
        private void RegisterGoToRegistrationCodePageMessage()
        {
            Messenger.Default.Register<int>(this, MainViewModel.GoToRegistrationCodePageMessageToken, p =>
            {
                GoToPage(new RegistrationCodePage());
            });
        }
    }
}
